<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
  <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
  <style type="text/css">
    .layui-form-selected dl {
        display: block;
        z-index: 99999999;
    }
    .file {
    position: relative;
    display: inline-block;
    background: #D0EEFF;
    border: 1px solid #99D3F5;
    border-radius: 4px;
    padding: 4px 12px;
    overflow: hidden;
    color: #1E88C7;
    text-decoration: none;
    text-indent: 0;
    line-height: 20px;
    }
    .file input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
        left: 0;
        width: 100px;
        height: 29px;

    }
    .file:hover {
        background: #AADFFD;
        border-color: #78C3F3;
        color: #004974;
        text-decoration: none;
    }
  </style>
</head>
<body style="padding:10px;">
  <div class="tplay-body-div">
    <div class="layui-tab">
      <ul class="layui-tab-title">
        <li><a href="{:url('admin/goods/index')}" class="a_menu">商品管理</a></li>
        <li class="layui-this">新增商品</li>
      </ul>
    </div> 
    <div style="margin-top: 20px;">
    </div>
    <form class="layui-form" id="admin" enctype="multipart/form-data">
      
      <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block" style="max-width:600px;">
          <input name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" type="text" {notempty name="$goods.title"}value="{$goods.title}"{/notempty}>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block" style="max-width:600px;">
          <input name="number_sn" autocomplete="off" placeholder="编号" class="layui-input" type="text" {notempty name="$goods.number_sn"}value="{$goods.number_sn}"{/notempty}>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">品牌</label>
        <div class="layui-input-block" style="max-width:600px;">
          <select name="brand_id" lay-filter="aihao">
            <option value="">请选择品牌</option>
            {volist name="$brand_list" id="vo"}
            <option value="{$vo.id}" {notempty name="$goods.brand_id"}{eq name="$goods.brand_id" value="$vo.id"} selected=""{/eq}{/notempty}>{$vo.name}</option>
            {/volist}
          </select>
        </div>
      </div>

      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block" style="max-width:600px;">
          <textarea placeholder="请输入内容" class="layui-textarea" name="description">{notempty name="$goods.description"}{$goods.description}{/notempty}</textarea>
        </div>
      </div>


      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block" style="max-width:1000px;">
          <textarea placeholder="请输入内容" class="layui-textarea" name="content" id="container" style="border:0;padding:0">{notempty name="$goods.content"}{$goods.content}{/notempty}</textarea>
        </div>
      </div>
      

      <div class="layui-form-item">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-block" style="max-width:600px;">
          <select name="goods_cate_id" lay-filter="aihao">
            <option value="">请选择分类</option>
            {volist name="$cates" id="vo"}
            <option value="{$vo.id}" {notempty name="$goods.goods_cate_id"}{eq name="$goods.goods_cate_id" value="$vo.id"} selected=""{/eq}{/notempty}>{$vo.str}{$vo.name}</option>
            {/volist}
          </select>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">Pdf</label>
        <div class="layui-input-block" style="max-width:600px;float: left;margin-left: 0px;margin-right: 10px;">
          <a href="javascript:;" class="file" >选择Pdf附件
              <input type="file" name="pdf" multiple="multiple"  onchange="handleFiles()">
              <input type="hidden" name="pdf_url" id="pdf_url" {notempty name="$goods.pdf_url"} value="{$goods.pdf_url}" {/notempty}>
          </a>
        </div>
        <div class="layui-form-mid layui-word-aux" style="height: 30px;line-height: 30px;padding: 0px !important;" id="pdf_html">{notempty name="$goods.pdf_url"}{$goods.pdf_url|geturl}{else}&nbsp;{/notempty}</div>
      </div>

      <div class="layui-upload" id="upload-thumb">
        <label class="layui-form-label">缩略图</label>
        <button type="button" class="layui-btn" id="thumb">上传图片</button>
        <div class="layui-upload-list">
          <label class="layui-form-label"></label>
          <img class="layui-upload-img" id="demo1" width="150" height="150" {notempty name="$goods.thumb"}src="{$goods.thumb|geturl}"{/notempty}>{notempty name="$goods.thumb"}<input type="hidden" name="thumb" value="{$goods.thumb}">{/notempty}
          <p id="demoText"></p>
        </div>
      </div>


      {notempty name="$goods"}
      <input type="hidden" name="id" value="{$goods.id}">
      {/notempty}
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
      
    </form>


    <script src="__PUBLIC__/layui/layui.js"></script>
    <script src="__PUBLIC__/jquery/jquery.min.js"></script>
    <!-- <script>
        var message;
        layui.config({
            base: '__JS__/',
            version: '1.0.1'
        }).use(['app', 'message'], function() {
            var app = layui.app,
                $ = layui.jquery,
                layer = layui.layer;
            //将message设置为全局以便子页面调用
            message = layui.message;
            //主入口
            app.set({
                type: 'iframe'
            }).init();
        });
    </script> -->
    <script>

    // function handleFiles()
    // {
    //   var obj = document.getElementById("pdf");
    //   var len = obj.files.length;
    //   for (var i = 0; i < len; i++) {
    //     var temp = obj.files[i].name;
    //     document.getElementById('pdf_html').innerHTML = temp;
    //   }

    //   uploadPic();
    // }

    function handleFiles()
    {
      var form = document.getElementById('admin'), 
        formData = new FormData(form); 
        $.ajax({ 
         url:"{:url('admin/common/pdf_upload')}", 
         type:"post", 
         data:formData, 
         processData:false, 
         contentType:false, 
         dataType:'json',
         success:function(res)
         { 
            //console.log(res.data.id);
            if(res.code == 0)
            {
              var src_url = res.data.src.split(".");
              if(src_url[1] == 'pdf')
              {
                document.getElementById('pdf_html').innerHTML = res.data.src;
                document.getElementById('pdf_url').value = res.data.id;
              }else
              {
                layer.msg("这不是一个有效的pdf附件");
              }
            }else
            {
              layer.msg(res.msg);
            }
         }, 
         error:function(err){ 
            layer.msg("网络连接失败,稍后重试");
         } 
        }) 
    }

    layui.use('upload', function(){
      var upload = layui.upload;


      //执行实例
      var uploadInst = upload.render({
        elem: '#thumb' //绑定元素
        ,url: "{:url('common/upload')}" //上传接口
        ,data:{use:'goods_thumb'}
        ,done: function(res){
          //上传完毕回调
          if(res.code == 2) {
            $('#demo1').attr('src',res.src);
            $('#upload-thumb').append('<input type="hidden" name="thumb" value="'+ res.id +'">');
          } else {
            layer.msg(res.msg);
          }
        }
        ,error: function(){
          //请求异常回调
          //演示失败状态，并实现重传
          var demoText = $('#demoText');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
          demoText.find('.demo-reload').on('click', function(){
            uploadInst.upload();
          });
        }
      });



    });


    </script>
    <script>
      layui.use(['layer', 'form'], function() {
          var layer = layui.layer,
              $ = layui.jquery,
              form = layui.form;
          $(window).on('load', function() {
              form.on('submit(admin)', function(data) {
                  $.ajax({
                      url:"{:url('admin/goods/publish')}",
                      data:$('#admin').serialize(),
                      type:'post',
                      async: false,
                      success:function(res) {
                          if(res.code == 1) {
                              layer.alert(res.msg, function(index){
                                location.href = res.url;
                              })
                          } else {
                              layer.msg(res.msg);
                          }
                      }
                  })
                  return false;
              });
          });
      });
    </script>

    <!-- 加载编辑器的容器 -->
    <script id="container" name="content" type="text/plain">
        这里写你的初始化内容
    </script>
    <!-- 配置文件 -->
    <script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.all.js"></script>
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        var ue = UE.getEditor('container');
    </script>
  </div>
</body>
</html>